﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PPI.Data.Utility
{
    public class DUtility_Asset_XMissionStructure
    {
        public static void DuplicateByInspection(Int32 inspectionId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_DuplicateByInspection", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@InspectionResultId", ParameterDirection.Input, false, inspectionId));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void DuplicateByVisit(Int32 visitId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_DuplicateByVendor", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@vendorId", ParameterDirection.Input, false, visitId));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void AssociateWithVisit(Int32 visitId, Int32 utilityId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_AssociateWithVendor", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@visitFk", ParameterDirection.Input, false, visitId));
                if(utilityId!= Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@utilityId", ParameterDirection.Input, false, utilityId));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@utilityId", ParameterDirection.Input, true, DBNull.Value));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }


        }

        public static void AssociateWithInspection(Int32 inspectionId, Int32 utilityId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_AssociateWithInspection", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@inspectionFk", ParameterDirection.Input, false, inspectionId));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@utilityId", ParameterDirection.Input, false, utilityId));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }


        }

        public static void AutolinkInspection(double maxdistanceInMts, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_AutoLinkInspections", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateFloatParameter("@maxdistanceInMts", ParameterDirection.Input, false, maxdistanceInMts));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void AutolinkVendor(double maxdistanceInMts,string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructures_AutoLinkVendors", DBHelper.GetConnection(connectionKey));

                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateFloatParameter("@maxdistanceInMts", ParameterDirection.Input, false, maxdistanceInMts));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static DataSet SelectAll(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Utility_Asset_XMissionStructures_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Utility_Asset_XMissionStructures_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet SelectAllNearByLatitudeLongitude(double latitude, double longitude, string connectionKey )
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Utility_Asset_XMissionStructures_NearByLatitudeLongitude", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add(DBHelper.CreateFloatParameter("@startLatitude", ParameterDirection.Input, false, latitude));
                da.SelectCommand.Parameters.Add(DBHelper.CreateFloatParameter("@startLongitude", ParameterDirection.Input, false, longitude));
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Utility_Asset_XMissionStructures_NearByLatitudeLongitude", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }
    }
}
